package it.tukano.swingdeco.joptionpane;

import java.awt.Component;
import java.awt.Dimension;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * JOptionPane shortcuts
 * @author pgi
 */
public class JOptionPaneExt {

    /**
     * Show a message dialog.
     * @param parent the parent of the dialog
     * @param message the message to display. It handles long messages.
     */
    public static void showMessageDialog(Component parent, String message) {
        if(message.length() > 30) {
            showLongMessageDialog(parent, message);
        } else {
            showShortMessageDialog(parent, message);
        }
    }

    public static void showMessageDialog(Component parent, Throwable t) {
        StringWriter out = new StringWriter();
        PrintWriter writer = new PrintWriter(out);
        t.printStackTrace(writer);
        showLongMessageDialog(parent, out.toString());
    }

    private static void showShortMessageDialog(Component parent, String message) {
        JOptionPane.showMessageDialog(parent, message);
    }

    private static void showLongMessageDialog(Component parent, String message) {
        JTextArea area = new JTextArea(message);
        area.setEditable(false);
        JScrollPane scroller = new JScrollPane(area);
        scroller.setPreferredSize(new Dimension(300, 200));
        JOptionPane.showMessageDialog(parent, scroller);
    }
}
